﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

using CommonLib;
using Hik.Communication.Scs.Client;
using Hik.Communication.Scs.Communication.EndPoints.Tcp;
using Hik.Communication.Scs.Communication.Messages;


namespace ClientApp
{
    public class NormalClient
    {
        
        private int _messageCount;
        private Stopwatch _stopwatch;
        
        private IScsClient client;
        private System.Timers.Timer aTimer;
        //Sample data
        private Tracking objTracking;
        private int _deviceId;

        public NormalClient(int deviceId)
        {
            _deviceId = deviceId;
        }

        public void Run()
        {
            //Console.WriteLine("Press enter to connect to server and send " + Consts.MessageCount + " messages.");
            //Console.ReadLine();

            using (var client = ScsClientFactory.CreateClient(new ScsTcpEndPoint("127.0.0.1", 10033)))
            {
                client.WireProtocol = new TrackingWireProtocol(); //Set custom wire protocol!
                //client.MessageReceived += client_MessageReceived;
                client.Connect();

                for (var i = 0; i < Consts.MessageCount; i++)
                {
                    //Sample data
                    Tracking objTracking = new Tracking();
                    byte[] data = objTracking.CreateSampleDataBytes();

                    client.SendMessage(new TrackingDataMessage(data));
                }

                //Console.WriteLine("Press enter to disconnect from server");
                //Console.ReadLine();
            }
        }

        public void RunWithTimer(int interval)
        {
            //Console.WriteLine("Press enter to connect to server and say Hello world!");
            //Console.ReadLine();

            client = ScsClientFactory.CreateClient(new ScsTcpEndPoint("127.0.0.1", 10033));
            client.WireProtocol = new TrackingWireProtocol(); //Set custom wire protocol!
            //client.MessageReceived += client_MessageReceived;
            client.Connect();

            //Sample data
            objTracking = new Tracking();

            //Console.WriteLine("Press enter to disconnect from server");
            //Console.ReadLine();

            // Create a timer with a ten second interval.
            aTimer = new System.Timers.Timer();

            // Hook up the Elapsed event for the timer.
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

            // Set the Interval to 1 seconds (1000 milliseconds).
            aTimer.Interval = interval;
            aTimer.Enabled = true;
            //GC.KeepAlive(aTimer);
            //Console.WriteLine("Press enter to disconnect from server");
            //Console.ReadLine();
        }

        // Specify what you want to happen when the Elapsed event is 
        // raised.
        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            //Tracking objTracking = new Tracking();
            byte[] data = objTracking.CreateSampleDataBytes(_deviceId);
            TrackingDataMessage mess = new TrackingDataMessage(data);
            client.SendMessage(mess);
            Console.WriteLine("The client sent a message: " + mess.ToString());
        }

        public void client_MessageReceived(object sender, MessageEventArgs e)
        {
            Console.WriteLine("The server sent a message: " + ((TrackingDataMessage)e.Message).ToString());
            ++_messageCount;

            if (_messageCount == 1)
            {
                _stopwatch = Stopwatch.StartNew();
            }
            else if (_messageCount == Consts.MessageCount)
            {
                _stopwatch.Stop();
                Console.WriteLine(Consts.MessageCount + " message is received in " + _stopwatch.Elapsed.TotalMilliseconds.ToString("0.000") + " ms.");
            }
        }
    }
}
